import axios from 'axios';
import { ElMessage } from 'element-plus';
import router from "./router";
const http = axios.create({
    baseURL: 'http://localhost:80/admin/api'
})
// 请求头拦截器
http.interceptors.request.use(function (config) {
    // 把token在传递服务器
    if(localStorage.token){
        config.headers.Authorization = 'Bearer ' + localStorage.token
    }
    
    return config;
  }, function (error) {
    return Promise.reject(error);
  });
// 响应拦截器
http.interceptors.response.use(res => {
    return res
}, err => {
    if (err.response.data.message) {
        ElMessage.error(err.response.data.message);
    }
    if(err.response.status===401){
        router.push('/login')
    }
    return Promise.reject(err)
})



export default http